$(function(){
    var permission_datagrid =  $("#permission_datagrid");
    var permission_dialog = $("#permission_dialog");
    var permission_form = $('#permission_form');
    //permission_datagrid 数据表初始化

    permission_datagrid.datagrid({
        url:'/permission/list',
        striped:"true",
        pagination:"true",
        singleSelect:"true",
        fit:"true",
        fitColumns:"true",
        toolbar: '#permission_toolbar',
        columns:[[
            {field:'x',checkbox:true},
            {field:'id',title:'编号',width:100},
            {field:'name',title:'权限名称',width:100},
            {field:'resource',title:'权限表达式',width:100}
        ]]
    })
    //permission_datagrid 数据表初始化-------end

    var cmdObj = {
        load:function(){
            $.messager.confirm('确认','您确认想要加载权限吗？',function(r){
                if (r){
                    $.get("/permission/load",function(data) {
                        if (data.success) {
                            permission_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            })
        },
        delete:function(){
            //判断是否选中
            var row = permission_datagrid.datagrid("getSelected")
            if(row == null){
                $.messager.alert('温馨提示',"请选中需要删除的权限");
                return ;
            }

            $.get("/permission/delete?id="+row.id,function(data){
                if (data.success){
                    $.messager.alert('温馨提示',data.msg);
                    permission_datagrid.datagrid("reload")
                }else{
                    $.messager.alert('温馨提示',data.msg);
                }

            })
        }
    }

    //代码优化
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

})